<?php

namespace app\controller\Api\V1\Client;

use app\helper\GetProductInfo;
use app\model\GameAuthorization;
use app\model\GameOfficialResources;
use app\model\GameResourcesRelevance;
use app\model\GameVersionRelevance;
use app\request\Resources;
use Exception;
use Illuminate\Support\Str;
use support\Request;
use support\Response;

class ResourcesController
{

    protected GetProductInfo $getProductInfoRe;


    public function __construct()
    {
        $this->getProductInfoRe = GetProductInfo::getInstance();
    }


    /**
     * 获取资源下载信息
     *
     * @param Request $request
     * @return Response
     * @throws Exception
     */
    public function Download(Request $request): Response
    {

        $validate = new Resources;

        if (!$validate->scene('download')->check($request->all())) {
            throw new Exception($validate->getError(), 500);
        }

        if ($request->input('release_type') == 1) {
            $merchantInfo = $this->getProductInfoRe->GetMerchantPlatform();
            $product = $merchantInfo['ulid'];
            $user = $request->input('mch_id');

            $gameAuthInfo = GameAuthorization::query()->where('ulid', $request->input('game_id'))
                ->where('merchant', $user)->first();

            if (!$gameAuthInfo) {
                throw new Exception('游戏不存在', 500);
            }
        } else {
            $developerInfo = $this->getProductInfoRe->GetDeveloperPlatform();
            $product = $developerInfo['ulid'];
            $user = $request->input('open_id');
        }

        $gameVersion = GameVersionRelevance::query()->has('VersionBind')->where('game', $request->input('game_id'))
            ->where('product', $product)->where('user', $user)->first();
        if (!$gameVersion) {
            throw new Exception('游戏版本不存在', 500);
        }

        // 商户自定义版本
        $customFirstPackageHotInfo = GameResourcesRelevance::query()->has('ResourceBind')->where('game', $request->input('game_id'))
            ->where('product', $product)->where('user', $user)->where('type', 1)
            ->where('update_type', 1)->latest()->first();
        $customResocureInfo = GameResourcesRelevance::query()->has('ResourceBind')->where('game', $request->input('game_id'))
            ->where('product', $product)->where('user', $user)->where('type', 2)
            ->where('update_type', 1)->latest()->first();

        $hot = [];
        if ($customFirstPackageHotInfo) {
            $baseUrl = Str::replace('first-packages', 'de-first-packages', Str::before($customFirstPackageHotInfo->ResourceBind->url, '.zip'));

            $hot = [
                'ulid' => $customFirstPackageHotInfo->ResourceBind->ulid,
                'type' => $customFirstPackageHotInfo->ResourceBind->type,
                'update_type' => $customFirstPackageHotInfo->ResourceBind->update_type,
                'url' => $customFirstPackageHotInfo->ResourceBind->url,
                'base_url' => $baseUrl,
                'title' => $customFirstPackageHotInfo->ResourceBind->title,
                'secret' => $gameVersion->VersionBind->secret ?? null,
            ];
        }

        $res = [];
        if ($customResocureInfo) {
            $baseUrl = Str::replace('miniport-packages', 'de-miniport-packages', Str::before($customResocureInfo->ResourceBind->url, '.zip'));

            $res = [
                'ulid' => $customResocureInfo->ResourceBind->ulid,
                'type' => $customResocureInfo->ResourceBind->type,
                'update_type' => $customResocureInfo->ResourceBind->update_type,
                'url' => $customResocureInfo->ResourceBind->url,
                'base_url' => $baseUrl,
                'title' => $customResocureInfo->ResourceBind->title,
                'secret' => $gameVersion->VersionBind->secret ?? null,
            ];
        }

        $result = [
            'hot' => $hot,
            'res' => $res,
        ];

        // 官方版本
        $officialResourceInfo = GameOfficialResources::query()->has('TypeBind')->has('EngineBind')
            ->where('game_type', $request->input('game_type'))
            ->where('state', 1)->where('type', 1)
            ->latest('version')->first();

        if (!$officialResourceInfo) {
            $official = [];
        } else {
            $officialResourceInfo = $officialResourceInfo->toArray();

            $official = [
                'version_number' => $officialResourceInfo['version'],
                'version_resource' => $officialResourceInfo['url'] . '/',
            ];
        }

        return jsonRe([
            'official' => $official,
            'custom' => $result,
        ]);

    }

}